package com.efast.dmom.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.dmom.mes.dao.entity.WmsSpareOutInboundLogDao;
import com.efast.dmom.mes.entity.EquipSpareCutterOutboundHistoryEntity;
import com.efast.dmom.mes.entity.EquipSpareInboundEntity;
import com.efast.dmom.mes.entity.EquipSpareOutboundEntity;
import com.efast.dmom.mes.entity.WmsSpareOutInboundLogEntity;
import com.efast.dmom.mes.service.WmsSpareOutInboundLogService;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;
import com.efast.dmom.mes.util.WmsOrderStatusUtil;

@Service
@Transactional
public class WmsSpareOutInboundLogServiceImpl implements WmsSpareOutInboundLogService {

	@Autowired
	private WmsSpareOutInboundLogDao wmsSpareOutInboundLog;

	@Override
	public void addSpareInboundLog(List<EquipSpareInboundEntity> spareList, String operation) {
		if (spareList != null && spareList.size() > 0) {
			List<WmsSpareOutInboundLogEntity> entityList = new ArrayList<WmsSpareOutInboundLogEntity>();
			List<EquipSpareCutterOutboundHistoryEntity> historyEntityList = new ArrayList<EquipSpareCutterOutboundHistoryEntity>();
			for (EquipSpareInboundEntity data : spareList) {
				WmsSpareOutInboundLogEntity logEntity = new WmsSpareOutInboundLogEntity();
				if (data.getSpareCategory().equals("刀具") && data.getCutterType().equals("刀具-有编号")) {
					if (data.getStorageQuality() == 1) {
						EquipSpareCutterOutboundHistoryEntity hisEntity = new EquipSpareCutterOutboundHistoryEntity();
						hisEntity.setSpareOutInboundId(data.getId());
						hisEntity.setSpareOutInboundNo(data.getInboundNumber());
						hisEntity.setWarehouseId(data.getWarehouseId());
						hisEntity.setSpareId(data.getSpareId());
						hisEntity.setOperation(operation);
						hisEntity.setSpareOutInboundQuality(0);
						hisEntity.setCreationTime(TimeUtil.getLocalDateTime());
						hisEntity.setCreator(SpringWebUtils.getRequestUser());
						hisEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
						hisEntity.setBeforeStatus(data.getUsedStatus());
						hisEntity.setAfterStatus(WmsOrderStatusUtil.SPARE_ENABLE_STATUS);
						historyEntityList.add(hisEntity);
					}
				}
				logEntity.setSpareOutInboundId(data.getId());
				logEntity.setSpareOutInboundNo(data.getInboundNumber());
				logEntity.setWarehouseId(data.getWarehouseId());
				logEntity.setSpareId(data.getSpareId());
				logEntity.setOperation(operation);
				if (data.getSpareCategory().equals("刀具") && data.getCutterType().equals("刀具-有编号")) {
					if (data.getStorageQuality() == 1) {
						logEntity.setSpareOutInboundQuality(0);
						logEntity.setUnitPrice(0);
					} else {
						logEntity.setSpareOutInboundQuality(data.getInboundQuantity());
						logEntity.setUnitPrice(data.getUnitPrice());
					}
				} else {
					logEntity.setSpareOutInboundQuality(data.getInboundQuantity());
					logEntity.setUnitPrice(data.getUnitPrice());
				}
				logEntity.setCreationTime(TimeUtil.getLocalDateTime());
				logEntity.setCreator(SpringWebUtils.getRequestUser());
				logEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
				logEntity.setDescription(data.getDescription());
				entityList.add(logEntity);
			}
			if (entityList != null && entityList.size() > 0) {
				wmsSpareOutInboundLog.saveBatch(entityList);
			}
			if (historyEntityList != null && historyEntityList.size() > 0) {
				wmsSpareOutInboundLog.saveBatchByCutter(historyEntityList);
			}
		}
	}

	@Override
	public void addSpareOutboundLog(List<EquipSpareOutboundEntity> spareList, String operation) {
		if (spareList != null && spareList.size() > 0) {
			List<WmsSpareOutInboundLogEntity> entityList = new ArrayList<WmsSpareOutInboundLogEntity>();
			List<EquipSpareCutterOutboundHistoryEntity> historyEntityList = new ArrayList<EquipSpareCutterOutboundHistoryEntity>();
			for (EquipSpareOutboundEntity data : spareList) {
				WmsSpareOutInboundLogEntity logEntity = new WmsSpareOutInboundLogEntity();
				if (data.getSpareCategory().equals("刀具") && data.getCutterType().equals("刀具-有编号")) {
					if (data.getOutboundReason().equals("刀具打磨")) {
						EquipSpareCutterOutboundHistoryEntity hisEntity = new EquipSpareCutterOutboundHistoryEntity();
						hisEntity.setSpareOutInboundId(data.getId());
						hisEntity.setSpareOutInboundNo(data.getOutboundNumber());
						hisEntity.setWarehouseId(data.getWarehouseId());
						hisEntity.setSpareId(data.getSpareId());
						hisEntity.setOperation(operation);
						hisEntity.setSpareOutInboundQuality(0);
						hisEntity.setCreationTime(TimeUtil.getLocalDateTime());
						hisEntity.setCreator(SpringWebUtils.getRequestUser());
						hisEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
						hisEntity.setBeforeStatus(WmsOrderStatusUtil.SPARE_ENABLE_STATUS);
						hisEntity.setAfterStatus(WmsOrderStatusUtil.SPARE_NOENABLE_STATUS);
						historyEntityList.add(hisEntity);
					} else if (data.getOutboundReason().equals("生产领用")) {
						EquipSpareCutterOutboundHistoryEntity hisEntity = new EquipSpareCutterOutboundHistoryEntity();
						hisEntity.setSpareOutInboundId(data.getId());
						hisEntity.setSpareOutInboundNo(data.getOutboundNumber());
						hisEntity.setWarehouseId(data.getWarehouseId());
						hisEntity.setSpareId(data.getSpareId());
						hisEntity.setOperation(operation);
						hisEntity.setSpareOutInboundQuality(0);
						hisEntity.setCreationTime(TimeUtil.getLocalDateTime());
						hisEntity.setCreator(SpringWebUtils.getRequestUser());
						hisEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
						hisEntity.setBeforeStatus(WmsOrderStatusUtil.SPARE_ENABLE_STATUS);
						hisEntity.setAfterStatus(WmsOrderStatusUtil.SPARE_RECEIVE_STATUS);
						historyEntityList.add(hisEntity);
					}
				}
				logEntity.setSpareOutInboundId(data.getId());
				logEntity.setSpareOutInboundNo(data.getOutboundNumber());
				logEntity.setWarehouseId(data.getWarehouseId());
				logEntity.setSpareId(data.getSpareId());
				logEntity.setOperation(operation);
				if (data.getSpareCategory().equals("刀具") && data.getCutterType().equals("刀具-有编号")) {
					if (data.getOutboundReason().equals("刀具打磨") || data.getOutboundReason().equals("生产领用")) {
						logEntity.setSpareOutInboundQuality(0);
						logEntity.setUnitPrice(0);
					} else {
						logEntity.setSpareOutInboundQuality(data.getOutboundQuantity());
						logEntity.setUnitPrice(data.getUnitPrice());
					}
				} else {
					logEntity.setSpareOutInboundQuality(data.getOutboundQuantity());
					logEntity.setUnitPrice(data.getUnitPrice());
				}
				logEntity.setCreationTime(TimeUtil.getLocalDateTime());
				logEntity.setCreator(SpringWebUtils.getRequestUser());
				logEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
				logEntity.setDescription(data.getDescription());
				entityList.add(logEntity);
			}
			if (entityList != null && entityList.size() > 0) {
				wmsSpareOutInboundLog.saveBatch(entityList);
			}
			if (historyEntityList != null && historyEntityList.size() > 0) {
				wmsSpareOutInboundLog.saveBatchByCutter(historyEntityList);
			}
		}
	}

}
